﻿@model MvcCms.Store.Web.ViewModels.ShoppingCartViewModel
@{
    var hasPriceOptions = false;
    var priceOptions = Model.CartItems.Select(p => p.PricingOption);
    if (priceOptions.Count() > 0) { hasPriceOptions = true; }
    var shippingMethods = Model.ShippingRates.Select(sr => sr.ShippingMethod).OrderBy(sm => sm.Importance).ToList();
    int shippingMethodCount = shippingMethods.Count();
    int methodCounter = 0;
}
<h3>
    <em>Review</em> your cart:
</h3>
                                                    
@if (Model._Portal.PaymentTypesAllowed == "cc" || Model._Portal.PaymentTypesAllowed == "cc-ach")
{
    <p class="button">
    <a href="javascript:void(0);" class="cc-link" data-val="/@Model._Portal.URLAuthority/Checkout/AddressAndPayment">Checkout Credit Card &gt;&gt;</a>
    </p>
}
@if (Model._Portal.PaymentTypesAllowed == "ach" || Model._Portal.PaymentTypesAllowed == "cc-ach")
{
    <p class="button">
    <a href="javascript:void(0);" class="ach-link" data-val="/@Model._Portal.URLAuthority/Checkout/AddressAndPaymentACH">Checkout Checking &gt;&gt;</a>
    </p>
}                         

<div id="update-message">
</div>
<div id="cart-container">
    <table>
    <tr>
        <th>
            Product Name
        </th>
        <th>
            Price (each)
        </th>
    @if (hasPriceOptions)
    {
        <th>
            Price Option
        </th>
    }
        <th>
            Quantity
        </th>
        <th></th>
    </tr>
    @foreach (var item in Model.CartItems)
    {
        var price = item.Product.Price;
        if (item.PricingOption != null) { price = item.PricingOption.Price; }
        
        <tr id="row-@item.RecordID">
            <td>
            @if (item.PricingOption != null && item.PricingOption.RequiresNotes)
            {
                <a href="javascript:void(0);" title="@item.OrderNotes">package info...</a>
            }
            else
            {
                @Html.ActionLink(item.Product.Title, "Details", "Store", new { id = item.ProductID }, null)
            }
            </td>
            <td>
                @price.ToMoney()
            </td>
        @if (hasPriceOptions)
        {
            <td>
            @if (item.PricingOption != null)
            {
                @item.PricingOption.Title
            }
            </td>
        }
            <td id="item-count-@item.RecordID">
                @item.Count
            </td>
            <td>
                <a href="javascript:void(0);" class="RemoveLink" data-id="@item.RecordID">Remove from cart</a>
            </td>
        </tr>
    }
    <tr>
        <td>Shipping</td>
        <td></td>
        @if (hasPriceOptions)
        {
            <td></td>
        }
        <td></td>
        <td id="shipping-amount"></td>
    </tr>
    <tr>
        <td>Total</td>
        <td></td>
        @if (hasPriceOptions)
        {
            <td></td>
        }
        <td></td>
        <td id="cart-total">@Model.CartTotal.ToMoney()</td>
    </tr>
</table>
</div>
<div id="shippingMethod-container">
    <div class="shipping-instructions">      
    <div class="shipping-rates-container">
    @foreach (var method in shippingMethods)
    {
        
        <div class="shippingMethod-column">        
            <div class="shippingMethod-header">@method.Title</div>
        @{
        var rate = Model.ShippingRates.Where(r => r.ShippingMethodID == method.ShippingMethodID).FirstOrDefault();
        var rateDisplay = "";
        if (rate.Price.HasValue) { rateDisplay = "$" + rate.Price.ToMoney(); } else { rateDisplay = Math.Round(rate.PercentOfTotal.GetValueOrDefault(), 2) + "%"; }
            <div class="shippingMethod-price"><a id="csl-@methodCounter" class="ChooseShippingLink" data-id="@rate.ShippingRateID" href="javascript:void(0);">@rateDisplay</a></div>
        methodCounter += 1;
        }        

        </div>
    }
    </div>
<h3>Shipping methods and deadlines</h3>
<h4>Ground Shipping</h4>
All orders placed by 11:59 p.m. EST can be processed the following business day and can ship out to arrive within 7 business days. Shipping dates are automatically calculated based on desired arrival date and recipient's shipping address.
<h4>2nd Day Delivery</h4>
All orders placed by 11:59 p.m. EST can be processed the following business day and delivered 2 business days from that date.  For example,
<ul>
    <li>Orders placed on Monday can ship Tuesday to be delivered Thursday.</li>
    <li>Orders placed on Thursday can ship Friday to be delivered Tuesday.</li>
</ul>
    </div>
</div>
<script type="text/javascript" id="DocReady">
    var shippingSet = false;
    $(document).ready(function () {

        $('.cc-link').click(function () {
            if (!shippingSet) { alert("Please choose a shipping method"); return false; }
            location.href = $(this).attr("data-val");
        });
        $('.ach-link').click(function () {
            if (!shippingSet) { alert("Please choose a shipping method"); return false; }
            location.href = $(this).attr("data-val");
        });

        $('a[title]').qtip({
            position: {
                my: 'bottom right',  // Position my top left...
                at: 'top left', // at the bottom right of...
                target: 'mouse' // my target
            },
            style: {
                classes: "ui-tooltip-dark"
            }
        });

        $(".ChooseShippingLink").click(function () {
            $('#update-message').text("Selecting Shipping Method...");
            var methodId = $(this).attr("data-id");
            if (methodId != '') {
                $.post("/ShoppingCart/ChooseShippingMethod", { "id": methodId },
                    function (data) {
                        //apend new shipping info row
                        $('#shipping-amount').text(data.ShippingTotal);
                        $('#cart-total').text(data.CartTotal);
                        $('#update-message').text(data.Message);
                        shippingSet = true;
                 });
            }
        });

        $(".RemoveLink").click(function () {
            $('#update-message').text("Removing Cart Item...");
            var recordToDelete = $(this).attr("data-id");
            if (recordToDelete != '') {
                var jdata = {
                    id: recordToDelete
                };
                $.ajax({
                    url: "/ShoppingCart/RemoveFromCart",
                    type: "POST",
                    dataType: "text json",
                    data: jdata,
                    success: function (data) {
                        if (data.ItemCount == 0) {
                            $('#row-' + data.DeleteId).fadeOut('slow');
                        } else {
                            $('#item-count-' + data.DeleteId).text(data.ItemCount);
                        }
                        var methodRates = data.DTOShippingRates;
                        $.each(methodRates, function (i, item) {
                            $('#csl-' + i).attr('data-id', methodRates[i].ShippingRateID);
                            $('#csl-' + i).text(methodRates[i].RateDisplay);
                        })
                        $('#shipping-amount').text('');
                        $('#cart-total').text(data.CartTotal);
                        $('#update-message').text(data.Message);
                        $('#cart-status').text('Cart (' + data.CartCount + ')');
                        shippingSet = false;
                    }
                })
            }
        });
    });
</script>